//思路：双指针实现简单，去重复杂：注意点nums[i]==nums[i+1]与nums[i]==nums[i-1]的区别在于前者会遗漏三元组
//哈希表则是j的判断移动，如果仅nums[j]==nums[j-1]则会遗漏

import java.sql.Array;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        int[]nums={1,-1,-1,0};
        List<List<Integer>> result=threeSum(nums);
        for(List s:result){
            for (int i = 0; i < s.size(); i++) {
                System.out.println(s.get(i));
            }
        }
    }

    //双指针
    public static List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > 0) {
                return result;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                int sum=nums[i] + nums[left] + nums[right];
                if (sum> 0) {
                    right--;
                } else if (sum<0) {
                    left++;
                } else {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left++;
                    right--;
                }

            }
        }
        return result;
    }
    

}













